#!/bin/sh
cat -n new.log |
grep total_time |
grep Response |
head -n 3 |
awk 'BEGIN{FS="url\":\""} {print $2}' |
awk -F "s\",\"Response\"" '{print $1}' |
#以双引号作为分割符
#awk -F '"' '{print $NF,$1}' |
#以双引号作为分割符，打印最后一个字段和第一个字段
awk -F "\"" '{print $NF,$1}' |
awk -F " " 'BEGIN {printf "%65s \t %40s \t %6s \t %6s \t %6s \t %6s \n", "url", "出现次数", "总耗时", "平均耗时", "最小耗时", "最大耗时"}

{
  arr[$2]["all"] += 1;
  arr[$2]["sum"] += $1;
  #if (!arr[$2]["min"]) {
  if (arr[$2]["min"] == 0) {
      arr[$2]["min"] = $1
  };
  if (arr[$2]["min"] > $1) {
      arr[$2]["min"] = $1
  };
  if (arr[$2]["max"] < $1) {
      arr[$2]["max"] = $1
  }
} END
{
  for (i in arr) {
      printf "%65s \t %20s \t %6s \t %6s \t %6s \t %6s \n",
      i,
      arr[i]["all"],
      arr[i]["sum"],
      arr[i]["sum"]/arr[i]["all"],
      arr[i]["min"],
      arr[i]["max"]
  }
}'

#格式化排序后的数据
printf "%65s \t %40s \t %6s \t %6s \t %6s \t %6s \n" "url" "出现次数" "总耗时" "平均耗时" "最小耗时" "最大耗时" $(cat -n new.log | grep -E "https://dl-api|https://member-mall|https://ibeautycn|https://united|https://api-delivery|https://wesocial" | awk 'BEGIN{FS="url\":\""} {print $2}' | awk -F "s\",\"Response\"" '{print $1}' | awk -F "\"" '{print $NF,$1}'| awk -F " " ' {arr[$2]["all"] += 1;arr[$2]["sum"] += $1; if (arr[$2]["min"] == 0) {arr[$2]["min"] = $1}; if (arr[$2]["min"] > $1) {arr[$2]["min"] = $1}; if (arr[$2]["max"] < $1) {arr[$2]["max"] = $1}} END {for (i in arr) {printf "%65s \t %20s \t %6s \t %6s \t %6s \t %6s \n", i, arr[i]["all"], arr[i]["sum"], arr[i]["sum"]/arr[i]["all"], arr[i]["min"], arr[i]["max"]}}' | sort -n -k 3,3 -r | head -n 3)

#分析pv方法一
cat -n 2022-03-24.log | grep -E "http://upstream" | grep time | awk 'BEGIN{FS="\"time\":\""} {print $2}' | awk 'BEGIN{FS="\",\"message"} {print $1}' | awk -F " " '{print $1,substr($2,1,5)}' | sort | uniq -c | sort -k 1,1 -nr | head -n 5
#分析pv方法二
cat new.log | grep -E "http://upstream" | jq -r '.time' | awk -F " " '{print $1,substr($2,1,5)}' | sort | uniq -c | sort -k 1,1 -nr | head -n 5


#分析uv方法一
cat -n new.log | grep -E "http://upstream" | grep time | awk 'BEGIN{FS="\"time\":\""} {print $2}' | awk 'BEGIN{FS="\"],\"content-length\":"} {print $1}' | awk 'BEGIN{FS="\""} {print $1,$NF}' |sort | uniq -c | sort -k 1,1 -nr | head -n 10
#分析uv方法二
cat -n 2022-03-24.log |
grep -E "http://upstream" |
grep time | awk 'BEGIN{FS="\"time\":\""} {print $2}' |
awk 'BEGIN{FS="\"],\"content-length\":"} {print $1}' |
awk -F "\"" '{
  if($1){
    #以空格切割字符串，放到arr数组
    split($1,arr," ");
    date = arr[1];
    #截取1到5长度的字符串
    #time = substr(arr[2],1,5);
    time = arr[2];
  }
  if($NF){
    ip = $NF;
  }
  #拼接变量
  print date " " time " " ip;
}END{

}' |
sort |
uniq -c |
sort -k 1,1 -nr |
head -n 10


#安装jq
- 如果要在Windows上顺利的使用jq，需要先安装chocolatey。
- 以管理员身份待开cmd窗口，输入@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"，回车。
- 接着输入chocolatey install jq

#增加
grep 'container_name":"member-center' export-5042f592-8991-4d17-b445-ba335bfe7f20.json | grep '2022-05-06T' | grep '请求全部数据'| awk -F '"' '{print $7}' | awk -F '.' '{print $1}' | sort | uniq -c | sort -k 1

